from scapy.all import *
import binascii


def deal_rec_hex(a_packet):
    """
    结果用0x分割: ex: 0x31 0x32 0x33 0x32 0x31 0x33
    """
    raw_str = binascii.hexlify(b'' if a_packet.getlayer(Raw) is None else a_packet.getlayer(Raw).load).decode('ascii')
    format_raw_str = " ".join(['0x' + raw_str[index:index + 2] for index in range(0, len(raw_str), 2)])
    all_str = a_packet.sprintf("%IP.src%->%IP.dst%")
    return all_str + " data:" + format_raw_str

def deal_rec_normal(a_packet):
    return a_packet.sprintf("%IP.src%->%IP.dst% data:%Raw.load%")


filter_rule = 'tcp and src 172.168.1.91 and port 8080'
pkts = sniff(iface='eth0', filter=filter_rule, prn=deal_rec_normal)
